/* 
 * File:   tools.h
 * Author: Bram
 *
 * Created on 4 augustus 2014, 22:28
 */

#ifndef TOOLS_H
#define	TOOLS_H

#ifdef	__cplusplus
extern "C" {
#endif


#define INT_FROM_BYTES(byteBuffer,pos)          ((byteBuffer[pos+0] << 24) | (byteBuffer[pos+1] << 16) | (byteBuffer[pos+2] << 8) | (byteBuffer[pos+3] << 0))
#define INT_FROM_BYTES_REV(byteBuffer,pos)      ((byteBuffer[pos+0] << 0) | (byteBuffer[pos+1] << 8) | (byteBuffer[pos+2] << 16) | (byteBuffer[pos+3] << 24))
#define SHORT_FROM_BYTES(byteBuffer,pos)        ((byteBuffer[pos+0] << 8) | (byteBuffer[pos+1] << 0))
#define SHORT_FROM_BYTES_REV(byteBuffer,pos)    ((byteBuffer[pos+0] << 0) | (byteBuffer[pos+1] << 8))

#define INT_FROM_BYTES_INC(byteBuffer,pos)          ((byteBuffer[pos++] << 24) | (byteBuffer[pos++] << 16) | (byteBuffer[pos++] << 8) | (byteBuffer[pos++] << 0))
#define INT_FROM_BYTES_REV_INC(byteBuffer,pos)      ((byteBuffer[pos++] << 0) | (byteBuffer[pos++] << 8) | (byteBuffer[pos++] << 16) | (byteBuffer[pos++] << 24))
#define SHORT_FROM_BYTES_INC(byteBuffer,pos)        ((byteBuffer[pos++] << 8) | (byteBuffer[pos++] << 0))
#define SHORT_FROM_BYTES_REV_INC(byteBuffer,pos)    ((byteBuffer[pos++] << 0) | (byteBuffer[pos++] << 8))

#define INT_TO_BYTES(val, buffer, pos)          { buffer[pos] = (val >> 24); buffer[pos+1] = (val >> 16); buffer[pos+2] = (val >> 8); buffer[pos+3] = (val >> 0); }
#define INT_TO_BYTES_REV(val, buffer, pos)      { buffer[pos] = (val >> 0); buffer[pos+1] = (val >> 8); buffer[pos+2] = (val >> 16); buffer[pos+3] = (val >> 24); }
#define SHORT_TO_BYTES(val, buffer, pos)        { buffer[pos] = (val >> 8); buffer[pos+1] = (val >> 0); }
#define SHORT_TO_BYTES_REV(val, buffer, pos)    { buffer[pos] = (val >> 0); buffer[pos+1] = (val >> 8); }

#define INT_TO_BYTES_INC(val, buffer, pos)          { buffer[pos++] = (val >> 24); buffer[pos++] = (val >> 16); buffer[pos++] = (val >> 8); buffer[pos++] = (val >> 0); }
#define INT_TO_BYTES_REV_INC(val, buffer, pos)      { buffer[pos++] = (val >> 0); buffer[pos++] = (val >> 8); buffer[pos++] = (val >> 16); buffer[pos++] = (val >> 24); }
#define SHORT_TO_BYTES_INC(val, buffer, pos)        { buffer[pos++] = (val >> 8); buffer[pos++] = (val >> 0); }
#define SHORT_TO_BYTES_REV_INC(val, buffer, pos)    { buffer[pos++] = (val >> 0); buffer[pos++] = (val >> 8); }

#ifdef	__cplusplus
}
#endif

#endif	/* TOOLS_H */

